package com.hxd.otc.module.wallet.fragment;

import android.content.Intent;
import android.os.Bundle;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.base.BaseRecyclerFragment;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.wallet.activity.CoinRechargeActivity;
import com.hxd.otc.module.wallet.activity.CoinWithdrawActivity;
import com.hxd.otc.module.wallet.activity.OtcToNormalAccountActivity;
import com.hxd.otc.module.wallet.activity.WalletActivity;
import com.hxd.otc.module.wallet.adapter.WalletCurrencyAdapter;
import com.hxd.otc.module.wallet.bean.WalletCurrencyBean;
import com.hxd.otc.module.wallet.contract.WalletCurrencyContract;
import com.hxd.otc.module.wallet.presenter.WalletCurrencyPresenter;
import com.hxd.otc.utils.LogUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzy on 2018/9/21 0021.
 * description ${钱包币种}
 */
public class WalletCurrencyFragment extends BaseRecyclerFragment<WalletCurrencyPresenter, WalletCurrencyAdapter, WalletCurrencyBean>
        implements WalletCurrencyContract.IView, WalletCurrencyAdapter.OnItemListener {
    public static final int REQUEST_CODE_WITHDRAW = 1;//提币
    private int pos;

    @Override
    protected WalletCurrencyAdapter createAdapter() {
        return new WalletCurrencyAdapter(getContext(), getArguments().getInt("pos"));
    }

    @Override
    protected WalletCurrencyPresenter createPresenter() {
        return new WalletCurrencyPresenter(this);
    }

    @Override
    protected void initView() {
        pos = getArguments().getInt("pos");
        if (pos == 0) {
            mPresenter.setNorInfo(false);
        } else {
            mPresenter.setNorInfo(true);
        }
        super.initView();
    }

    @Override
    public boolean isEnableRefresh() {
        return false;
    }

    @Override
    protected void initData() {
        super.initData();
        if (pos == 1) {
            registerEventBus();
        }
        mAdapter.setOnItemListener(this);

    }

    List<WalletCurrencyBean> otcBeans;
    List<WalletCurrencyBean> norBeans;

    public void hideZeroAssests(int position) {
        LogUtils.e("==pos : " + pos + " , position : " + position);
        if (BaseApplication.isHideZeroAssests) {
            if (pos == 0) {
                otcBeans = new ArrayList<>();
                if (mPresenter.getOtcWalletInfoBeans() != null && mPresenter.getOtcWalletInfoBeans().size() > 0) {
                    if (UserHelper.isSilver()) {
                        for (int i = 0; i < mPresenter.getOtcWalletInfoBeans().size(); i++) {
                            if (mPresenter.getOtcWalletInfoBeans().get(i).volume != 0
                                    || (mPresenter.getOtcWalletInfoBeans().get(i).otcAdvertVolume
                                    + mPresenter.getOtcWalletInfoBeans().get(i).otcLockVolume) != 0
                                    || mPresenter.getOtcWalletInfoBeans().get(i).bailVolume != 0) {
                                otcBeans.add(mPresenter.getOtcWalletInfoBeans().get(i));

                            }
                        }
                    } else {
                        for (int i = 0; i < mPresenter.getOtcWalletInfoBeans().size(); i++) {
                            if (mPresenter.getOtcWalletInfoBeans().get(i).volume != 0
                                    || (mPresenter.getOtcWalletInfoBeans().get(i).otcAdvertVolume
                                    + mPresenter.getOtcWalletInfoBeans().get(i).otcLockVolume) != 0) {
                                otcBeans.add(mPresenter.getOtcWalletInfoBeans().get(i));

                            }
                        }
                    }

                    mAdapter.addAll(true, otcBeans);
                }
            } else if (pos == 1) {
                norBeans = new ArrayList<>();
                if (mPresenter.getNorWalletInfoBeans() != null && mPresenter.getNorWalletInfoBeans().size() > 0) {
                    for (int i = 0; i < mPresenter.getNorWalletInfoBeans().size(); i++) {
                        if (mPresenter.getNorWalletInfoBeans().get(i).volume != 0) {
                            norBeans.add(mPresenter.getNorWalletInfoBeans().get(i));
                        }
                    }
                    mAdapter.addAll(true, norBeans);
                }
            }
        } else {
            if (pos == 0) {
                mAdapter.addAll(true, mPresenter.getOtcWalletInfoBeans());
            } else {
                mAdapter.addAll(true, mPresenter.getNorWalletInfoBeans());
            }
        }
    }

    List<WalletCurrencyBean> otcSerchCoins;
    List<WalletCurrencyBean> norSerchCoins;

    public void searchCoin(String coin,boolean isSerch) {

        if (pos == 0) {
            if (mPresenter.getOtcWalletInfoBeans() != null && mPresenter.getOtcWalletInfoBeans().size() > 0) {
                if (!isSerch){
                    mAdapter.addAll(true,mPresenter.getOtcWalletInfoBeans());
                    return;
                }
                otcSerchCoins = new ArrayList<>();
                for (int i = 0; i < mPresenter.getOtcWalletInfoBeans().size(); i++) {
                    if (mPresenter.getOtcWalletInfoBeans().get(i).getName().trim().contains(coin)) {
                        otcSerchCoins.add(mPresenter.getOtcWalletInfoBeans().get(i));
                    }
                }
                mAdapter.addAll(true, otcSerchCoins);
            }
        } else {
            if (mPresenter.getNorWalletInfoBeans() != null && mPresenter.getNorWalletInfoBeans().size() > 0) {
                norSerchCoins = new ArrayList<>();
                for (int i = 0; i < mPresenter.getNorWalletInfoBeans().size(); i++) {
                    if (mPresenter.getNorWalletInfoBeans().get(i).getName().trim().contains(coin)) {
                        norSerchCoins.add(mPresenter.getNorWalletInfoBeans().get(i));
                    }
                }
                mAdapter.addAll(true, norSerchCoins);
            }
        }
    }

    @Override
    public boolean isEnableLoadMore() {
        return false;
    }

    @Override
    public void lazyLoad() {
        if (isFirstVisible && mRootView != null) {
            isFirstVisible = false;
            if (getUserVisibleHint()) {
                showSpinnerDlg(getString(R.string.is_loading), false);
            }
            initView();
            initData();
            initEvent();
        } else {
            if (getUserVisibleHint() && UserHelper.isLogin()) {
                mPresenter.onRefresh();
            }
        }
    }


    @Override
    public void OnWalletIntoClick(int position) {
//        if (mPresenter.getOtcWalletInfoBeans() == null && mPresenter.getOtcWalletInfoBeans().size() <= 0 &&
//                mPresenter.getNorWalletInfoBeans() == null && mPresenter.getNorWalletInfoBeans().size() <= 0)
//            return;
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("walletInfo", (Serializable) mPresenter.getOtcWalletInfoBeans());
//        bundle.putInt("pos", position);
//        bundle.putSerializable("normalWalletInfo", (Serializable) ((WalletActivity) getActivity()).getNormalFragment().mPresenter.getNorWalletInfoBeans());
//        $startActivityForResult(OtcToNormalAccountActivity.class, 1, bundle, false);
    }

    @Override
    public void OnRechargeCoinClick(int position) {
        if (mPresenter.getNorWalletInfoBeans() == null && mPresenter.getNorWalletInfoBeans().size() <= 0)
            return;
//        Bundle bundle = new Bundle();
//        bundle.putString("coinType", mPresenter.getNorWalletInfoBeans().get(position).getName());
//        bundle.putString("coinId", mPresenter.getNorWalletInfoBeans().get(position).getId());
//        $startActivity(CoinRechargeActivity.class, bundle);
    }

    @Override
    public void OnEextractCoinClick(int position) {
        if (mPresenter.getNorWalletInfoBeans() == null && mPresenter.getNorWalletInfoBeans().size() <= 0)
            return;
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("walletCurrencyBean", mPresenter.getNorWalletInfoBeans().get(position));
//        $startActivityForResult(CoinWithdrawActivity.class, REQUEST_CODE_WITHDRAW, bundle, false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.onRefresh();
    }

    @Override
    public void onSuccedd() {
//        if (pos == 1) {
//            ((NewWalletFragment) getParentFragment()).setWalletCurrencyBeans((ArrayList<WalletCurrencyBean>) mPresenter.getWalletInfoBeans());
//        }
        dismissSpinnerDlg();
        if (BaseApplication.isHideZeroAssests) {
            hideZeroAssests(0);
            hideZeroAssests(1);
        }
    }

    @Override
    public void onLoadDataFail(boolean isRefresh, String msg) {
        super.onLoadDataFail(isRefresh, msg);
        dismissSpinnerDlg();
    }

    public void onNewIntent() {
        if (isAdded() && UserHelper.isLogin()) {
            mPresenter.loadData(true);
        }
    }

    /**
     * 更新 常规账户的数据
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(Event.NormalAccountEvent accountEvent) {
        if (pos == 1) {
            mPresenter.loadData(true);
        }
    }
}
